package com.buercorp.wangyu.socket.tcpdemo02;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 客户端发送数据并接收服务器返回的数据
 * @author liyiyu
 */
public class Client {
    public static void main(String[] args) throws IOException {
        // 创建socket对象，连接服务器
        Socket socket = new Socket("127.0.0.1", 13333);
        // 写出数据
        OutputStream os = socket.getOutputStream();
        String str = "客户端发送的数据";
        os.write(str.getBytes());

        // 写出结束标记
        socket.shutdownOutput();

        // 接收服务器回写的数据
        InputStream is = socket.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        int b;
        while ((b = isr.read()) != -1){
            System.out.println((char) b);
        }

        // 释放资源
        socket.close();
    }
}
